package srx.open.com.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.nio.Buffer;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import srx.open.com.R;
import srx.open.com.mode.ClassItem;
import srx.open.com.utils.Log;

/**
 *  课程分类选择界面.
 */
public class CourseClassActivity extends ExtendBaseActivity {
    private static final String TAG = "CourseClassActivity";
    public static final int RESULT_CLASS_STATE = 20151;
    // 跳转view的状态.
    private static final int JMP_FIRST_NUM = 0;
    private static final int JMP_SECOND_NUM = 1;
    private static final int JMP_THREE_NUM = 2;

    // 标题栏
    @Bind(R.id.left_lay)
    View left_lay;
    @Bind(R.id.back_iv)
    ImageView back_iv;
    @Bind(R.id.right_lay)
    View right_lay;
    @Bind(R.id.left_tv)
    TextView left_tv;

    // 搜索
    @Bind(R.id.search_et)
    EditText search_et;
    // 已经选择课程的..
    @Bind(R.id.select_course_tv)
    TextView select_course_tv;
    // 课程分类一
    @Bind(R.id.class_one_listview)
    ListView class_one_listview;
    // 课程分类二
    @Bind(R.id.class_two_listview)
    ListView class_two_listview;
    // 课程分类三
    @Bind(R.id.class_three_listview)
    ListView class_three_listview;

    private int jmpState = JMP_FIRST_NUM;
    private List<ClassItem> mClassOneList = new ArrayList<ClassItem>();
    private List<ClassItem> mClassTwoList = new ArrayList<ClassItem>();
    private List<ClassItem> mClassThreeList = new ArrayList<ClassItem>();
    private CourseAdapter mClassOneAdapter;
    private CourseAdapter mClassTwoAdapter;
    private CourseAdapter mClassThreeAdapter;
    private String saveClassName;

    private ArrayList<ClassItem> allClassItemLists = new ArrayList<ClassItem>() {
        {
            add(new ClassItem()); // 分类一
            add(new ClassItem()); // 分类二
            add(new ClassItem()); // 分类三
        }
    };

    @Override
    public void initAllDatas() {
        mClassOneList = mDataBaseHelper.queryClassifyOne();
    }

    @Override
    public void initAllViews() {
        setContentView(R.layout.activity_course_class_detailed);
        ButterKnife.bind(this);
        hideSoftInputView();
        // 初始化.
        initBundleDatas();
        initTitles();
        initListView();
    }

    private void initBundleDatas() {
        // 获取已经选择的地址.
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String selectAddr = extras.getString("selectClass");
            select_course_tv.setText(selectAddr);
        }
    }

    private void initTitles() {
        back_iv.setImageResource(R.drawable.addr_left_close);
        right_lay.setVisibility(View.GONE);
        left_tv.setText("添加课程分类信息");
    }

    private void initListView() {
        // 课程分类一
        mClassOneAdapter = new CourseAdapter(mClassOneList);
        class_one_listview.setAdapter(mClassOneAdapter);
        class_one_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                hideSoftInputView();
                if (JMP_FIRST_NUM == jmpState) {
                    // 更新数据.
                    ClassItem classOneItem = mClassOneList.get(position);
                    mClassTwoList = mDataBaseHelper.queryClassifyTwo(classOneItem.getOneId());
                    allClassItemLists.set(0, classOneItem); // 保存.
                    if (mClassTwoList.size() > 0) {
                        mClassTwoList.add(0, null);
                        saveClassName = classOneItem.getDescription();
                        left_tv.setText("" + saveClassName);
                        mClassTwoAdapter.setDataUpdate(mClassTwoList);
                        setJmpInSecondView();
                    } else {
                        setActivityResult(classOneItem);
                    }
                }
            }
        });
        // 课程分类二.
        mClassTwoAdapter = new CourseAdapter(mClassTwoList);
        class_two_listview.setAdapter(mClassTwoAdapter);
        class_two_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (JMP_SECOND_NUM == jmpState) {
                    // 更新/显示数据.
                    ClassItem classTwoItem = mClassTwoList.get(position);
                    if (classTwoItem == null) {
                        setActivityResult(allClassItemLists.get(0));
                        return;
                    }
                    allClassItemLists.set(1, classTwoItem); // 保存.
                    mClassThreeList = mDataBaseHelper.queryClassifyThree(classTwoItem.getTwoId());
                    if (mClassThreeList.size() > 0) {
                        mClassThreeList.add(0, null);
                        left_tv.setText("" + classTwoItem.getDescription());
                        mClassThreeAdapter.setDataUpdate(mClassThreeList);
                        setJmpInThreeView();
                    } else {
                        setActivityResult(classTwoItem);
                    }
                }
            }
        });
        // 课程分类三.
        mClassThreeAdapter = new CourseAdapter(mClassThreeList);
        class_three_listview.setAdapter(mClassThreeAdapter);
        class_three_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (JMP_THREE_NUM == jmpState) {
                    ClassItem classThreeItem = mClassThreeList.get(position);
                    if (classThreeItem == null) {
                        setActivityResult(allClassItemLists.get(1));
                        return;
                    }
                    allClassItemLists.set(2, classThreeItem); // 保存.
                    setActivityResult(classThreeItem);
                }
            }
        });
    }

    private void setJmpInSecondView() {
        Animation animationIn = AnimationUtils.loadAnimation(mContext, R.anim.addr_view_in);
        class_two_listview.setVisibility(View.VISIBLE);
        class_two_listview.setAnimation(animationIn);
        back_iv.setImageResource(R.drawable.addr_left_back);
        jmpState = JMP_SECOND_NUM;
    }

    private void setJmpInThreeView() {
        Animation animationIn = AnimationUtils.loadAnimation(mContext, R.anim.addr_view_in);
        class_three_listview.setVisibility(View.VISIBLE);
        class_three_listview.setAnimation(animationIn);
        jmpState = JMP_THREE_NUM;
    }

    public void setActivityResult(ClassItem classItem) {
        Intent intent = new Intent();
        //
        intent.putExtra("class1", allClassItemLists.get(0).getDescription());
        intent.putExtra("id1", allClassItemLists.get(0).getOneId());
        //
        intent.putExtra("class2", allClassItemLists.get(1).getDescription());
        intent.putExtra("id2", allClassItemLists.get(1).getTwoId());
        //
        intent.putExtra("class3", allClassItemLists.get(2).getDescription());
        intent.putExtra("id3", allClassItemLists.get(2).getThreeId());
        // 已经选择的课程.
        String selectClass = classItem.getDescription();
        intent.putExtra("selectClass", selectClass);
        Log.w(TAG + " selectClass:" + selectClass + " id:" + classItem.getTwoId() + " id:" + allClassItemLists.get(1).getTwoId());
        // 返回值.
        setResult(RESULT_CLASS_STATE, intent);
        finish();
    }

    @Override
    public void onBackPressed() {
        setJmpOutView();
    }

    /**
     *  返回.
     */
    @OnClick(R.id.left_lay)
    void leftLayOnClick(View view) {
        setJmpOutView();
    }

    private void setJmpOutView() {
        Animation animationOut = AnimationUtils.loadAnimation(mContext, R.anim.addr_view_out);
        switch (jmpState) {
            case JMP_FIRST_NUM:
                super.onBackPressed();
                break;
            case JMP_SECOND_NUM:
                class_two_listview.setVisibility(View.GONE);
                class_two_listview.setAnimation(animationOut);
                back_iv.setImageResource(R.drawable.addr_left_close);
                jmpState = JMP_FIRST_NUM;
                left_tv.setText("" + "添加课程分类信息");
                break;
            case JMP_THREE_NUM:
                class_three_listview.setVisibility(View.GONE);
                class_three_listview.setAnimation(animationOut);
                jmpState = JMP_SECOND_NUM;
                left_tv.setText("" + saveClassName);
                saveClassName = "";
                break;
        }
    }

    /**
     * 课程分类adapter.
     */
    public class CourseAdapter extends BaseAdapter {

        private LayoutInflater mInflater;
        private List<ClassItem> mAddrList;

        public CourseAdapter(List<ClassItem> addrList) {
            mInflater = LayoutInflater.from(mContext);
            mAddrList = addrList;
        }

        public void setDataUpdate(List<ClassItem> addrList) {
            mAddrList = addrList;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mAddrList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final MyViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.listitem_addr, parent,
                        false);
                holder = new MyViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (MyViewHolder) convertView.getTag();
            }
            //
            ClassItem province = mAddrList.get(position);
            if (province != null) {
                holder.name_tv.setText("" + province.getDescription());
            } else {
                holder.name_tv.setText("全部");
            }

            return convertView;
        }

        private class MyViewHolder {
            public TextView name_tv;

            public MyViewHolder(View v) {
                name_tv = (TextView) v.findViewById(R.id.name_tv);
            }
        }

    }

}
